package com.cn.learn.httpclient.util;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName ServletUtil
 * @Description
 * @Author JiJiang
 * @Date 2023/5/20 15:54
 * @Version 1.0
 */

public final class ServletUtil {


    /**
     * 获取请求参数对象
     *
     * @return
     */
    public static HttpServletRequest getServletRequest() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return servletRequestAttributes.getRequest();

    }


    /**
     * 获取请求参数
     * @param paramName
     * @return
     */
    public static Object getParam(String paramName) {
        HttpServletRequest servletRequest = getServletRequest();

        String method = servletRequest.getMethod();

        Object retData = null;

        //如果是get或delete方法
        if ("GET".equalsIgnoreCase(method) || "DELETE".equalsIgnoreCase(method)) {
            retData = servletRequest.getParameter(paramName);
        }
        //如果是put或post方法
        else if ("PUT".equalsIgnoreCase(method) || "POST".equalsIgnoreCase(method)) {

        } else {

        }
        return retData;


    }

}
